package com.ccdc.cbpc.bigdata.base.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({Docket.class})
@Profile({"!prod"})
public class SwaggerConfig {
    @Value("${swagger.basePackage:com.ccdc.cbpc.bigdata}")
    private String swaggerBasePackage;
    @Value("${swagger.title:后台接口地址}")
    private String title;
    @Value("${swagger.version:1.0.0}")
    private String version;
    @Value("${swagger.description:后台API}")
    private String description;
    @Value("${swagger.routes.contextRoot:}")
    private String contxtRoot;
    @Value("${swagger.routes.enableContextRoot:false}")
    private boolean enableContxtRoot;

    @Bean
    @Order(2147483647)
    @ConditionalOnMissingBean
    public Docket createRestApi() {
        return (new Docket(DocumentationType.SWAGGER_2)).apiInfo(this.apiInfo()).pathMapping(this.enableContxtRoot ? this.contxtRoot : "").select().apis(RequestHandlerSelectors.basePackage(this.swaggerBasePackage)).paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        return (new ApiInfoBuilder()).title(this.title).version(this.version).description(this.description).build();
    }
}
